package com.shen.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shen.commonutils.Result;
import com.shen.eduservice.entity.EduCourse;
import com.shen.eduservice.entity.vo.CourcePublishVo;
import com.shen.eduservice.entity.vo.CourseInfoVo;
import com.shen.eduservice.entity.vo.CourseQuery;
import com.shen.eduservice.service.EduChapterService;
import com.shen.eduservice.service.EduCourseDescriptionService;
import com.shen.eduservice.service.EduCourseService;
import com.shen.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author shenkaituo
 * @since 2020-06-21
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    //课程描述的注入
    @Autowired
    EduCourseService eduCourseService;

    //无条件分页查询
    @GetMapping("pageCourse/{current}/{limit}")
    //没什么用
    public Result pageCourse(@PathVariable("current") long current, @PathVariable("limit") long limit) {

        Page<EduCourse> page = new Page<>(current, limit);//都是long类型的
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        eduCourseService.page(page, queryWrapper);//根据结果查询 把结果放到page里面
        List<EduCourse> records = page.getRecords();
        long total = page.getTotal();
        return Result.ok().data("records", records).data("total", total);

    }

    //有条件分页查询
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public Result pageCourseConditon(@PathVariable("current") long current, @PathVariable("limit") long limit,
                                     @RequestBody(required = false) CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq("status", status);
        }
        Page<EduCourse> page = new Page<>(current, limit);
        eduCourseService.page(page, queryWrapper);
        List<EduCourse> records = page.getRecords();
        long total = page.getTotal();

        return Result.ok().data("records", records).data("total", total);
    }

    //添加课程基本信息
    @PostMapping("addCourceInfo")
    public Result addCourceInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        System.out.println(" this  courseid " + id);

        return Result.ok().data("courseId", id);
    }

    //根据课程id得到课程信息
    @GetMapping("getCourceInfo/{courseId}")
    public Result getCourceInfo(@PathVariable("courseId") String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourceInfo(courseId);
        return Result.ok().data("courseInfoVo", courseInfoVo);
    }

    //根据课程id修改课程信息
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String courseId = courseInfoVo.getId();
        eduCourseService.updateCourceInfo(courseInfoVo);
        return Result.ok().data("courseId", courseId);

    }

    //得到发布的信息
    @GetMapping("getPublishCourseInfo/{id}")
    public Result getPublishCourseInfo(@PathVariable String id) {
        CourcePublishVo courcePublishVo = eduCourseService.PublishCourseInfo(id);
        return Result.ok().data("publishCourse", courcePublishVo);
    }

    //修改status 改为Normal 表示课程已经发布
    @GetMapping("publishCourse/{id}")
    public Result publishCourse(@PathVariable String id) {

        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程状态为
        boolean result = eduCourseService.updateById(eduCourse);
        if (result) {
            return Result.ok();
        } else {
            return Result.error();
        }

    }

    //删除课程
    @DeleteMapping("deleteCourse/{id}")
    public Result deleteCource(@PathVariable String id) {
        System.out.println("delet course  conrolller");

        boolean flag = eduCourseService.removeCourse(id);


        if (flag) return Result.ok();
        else return Result.error();
    }


}




